package com.huangshi.app.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.util.AttributeSet;
import android.content.res.TypedArray;
import com.google.android.material.tabs.TabLayout;

import com.huangshi.app.R;
import com.huangshi.skinlib.SkinViewSupport;
import com.huangshi.skinlib.utils.SkinResources;

import androidx.annotation.NonNull;

public class MyTabLayout extends TabLayout implements SkinViewSupport {
    int tabIndicatorColorResId;
    int tabTextColorResId;

    public MyTabLayout(@NonNull Context context) {
        this(context,null,0);
    }
    public MyTabLayout(Context context, AttributeSet attrs){
        this(context,attrs,0);
    }

    public MyTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context,attrs,defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabLayout,defStyleAttr,0);
        tabIndicatorColorResId = a.getResourceId(R.styleable.TabLayout_tabIndicatorColor,0);
        tabTextColorResId = a.getResourceId(R.styleable.TabLayout_tabTextColor,0);
        a.recycle();
    }

    @Override
    public void applySkin() {
        if(tabIndicatorColorResId != 0){
            int tabIndicatorColor = SkinResources.getInstance().getColor(tabIndicatorColorResId);
            setSelectedTabIndicatorColor(tabIndicatorColor);
        }

        if(tabTextColorResId != 0){
            ColorStateList tabTextColor = SkinResources.getInstance().getColorStateList(tabTextColorResId);
            setTabTextColors(tabTextColor);
        }
    }
}
